
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Logging;

namespace Localization
{
	public class CompanyUserLocalizationSettingsBizRules : Syrinx.BaseBizRules
	{
		public void updateUserLocalizationSettings(IApplication app, IRuleContext context, object returnValue, object[] inputParams)
		{
			IBizObj user = inputParams[2] as IBizObj;
			if (user != null && user.isDirty("settings.translationId"))
			{
				app.appData.put("DA.Localization.UpdateUserLocalizationSettings", user);
			}
		}

		public IBizObj getUserLocalizationSettings(IApplication app, IRuleContext context, object returnValue, object[] inputParams)
		{
			IBizObj user = returnValue as IBizObj;
			if (user != null)
			{
				user.put("settings.id", user.get("id"));
				IBizObj settings = app.appData.get("DA.Localization.GetUserLocalizationSettings", user.get("settings")) as IBizObj;
				if (settings != null)
					user.put("settings.translationId", settings.get("translationId"));
			}
			return user;
		}

		public IBizObj getUserLocalizationSettings2(IApplication app, IRuleContext context, object returnValue, object[] inputParams)
		{
			IBizObj userSettings = returnValue as IBizObj;
			if (userSettings != null)
			{
				IBizObj settings = app.appData.get("DA.Localization.GetUserLocalizationSettings", userSettings) as IBizObj;
				if (settings != null)
				{
					userSettings.put("translationId", settings.get("translationId"));
					userSettings.put("iso6392", settings.get("iso6392"));
					if (settings.get("translationId") != null)
					{
						IBizObj culture = app.bizObjMgr.create("Culture");
						culture.put("browserCode", settings.get("iso6392"));
						culture.put("translationId", settings.get("translationId"));
						app.resMgr.CurrentCulture = culture;
					}
				}
			}
			return userSettings;
		}
	}
}
